package com.ln.lnzw.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.ln.lnzw.R;
import com.ln.lnzw.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者 : wlt
 * 创建时间:  2018-04-25
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:便民服务中每个服务的详情
 * In me the tiger sniffs the rose
 */

public class ServiceAppDetailActivity extends BaseActivity {

    Unbinder unbinder;

    @BindView(R.id.iv_back)
    ImageView iv_back;

    @BindView(R.id.tv_title_bar)
    TextView tv_title_bar;

    @BindView(R.id.wv_webview)
    WebView wvWebView;

    private WebSettings webSettings;

    private String url;

    private String name;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_app_details);
        unbinder = ButterKnife.bind(this);
        initData();
        loadWeb();
    }

    private void initData() {
        url = getIntent().getStringExtra("url");
        name = getIntent().getStringExtra("name");
    }

    private void loadWeb() {

        tv_title_bar.setText(name);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wvWebView.canGoBack()) {
                    wvWebView.goBack();
                }
                else{
                    finish();
                }
            }
        });

        if (wvWebView != null) {
            wvWebView.getSettings().setJavaScriptEnabled(true);
            wvWebView.setHorizontalScrollBarEnabled(false);//去掉横向滚动条
            webSettings = wvWebView.getSettings();
            webSettings.setSupportZoom(true);
            webSettings.setDomStorageEnabled(true);
//            webSettings.setAllowFileAccess(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setDisplayZoomControls(false);


            webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            webSettings.setUseWideViewPort(true);//设置webview推荐使用的窗口
            webSettings.setLoadWithOverviewMode(true);//设置webview加载的页面的模式

            /**
             * 用WebView显示图片，可使用这个参数 设置网页布局类型：
             * 1、LayoutAlgorithm.NARROW_COLUMNS ：适应内容大小
             * 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
             */
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            wvWebView.loadUrl(url);
            wvWebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);

                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);

                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    wvWebView.loadUrl(url);
                    return true;
                }
            });
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        wvWebView = null;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wvWebView.canGoBack()) {
            wvWebView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
